*******************************************
** S-RLS Panel F2F Survey 2024 **
** Data Cleaning File     	   **                                     
*******************************************
clear all
set more off
set maxvar 10000
//ssc install missings 
	
use "${dir}/raw_public/panel_F2F2024_deidentified.dta", clear  

*****************************RENAMING***************************
** Omitted sections need no changes
** Omitted variables are PII or DNE in data
** 

** Survey Section 1
rename q110 q1_14
rename q109 q1_13
rename q107 q1_11
rename q106 q1_10

drop q1_11 // PII 

** Survey Section 3
rename q303c q304c
rename q303c_other q304c_other
rename q303c_other_C1 q304c_other_C1
drop q305c //Not in this survey version
drop q305d //Not in this survey version


** Survey Section 4
rename q405 q407
rename q404b q406_months
rename q404a q406_years
rename q403_other_C1 q405_other_C1
rename q403_other q405_other
rename q403 q405
rename q402a q404a

rename q401b_other_C1 q403a_other_C1
rename q401b_other q403a_other
rename q401b q403a
rename q401a_other_C1 q403_other_C1
rename q401a_other q403_other
rename q401a q403
rename q402 q404_value
rename q401 q402

forval i = 1(1)4{
	rename q401_a_`i' q407_a_`i'
	rename q402a_a_`i' q407a_a_`i'
	rename q402b_a_`i' q407b_a_`i'
	rename q403_a_`i' q407c_a_`i'
	rename q403_a_other_C1_`i' q407c_a_other_C1_`i' 
	rename q404a_a_`i' q407a_a_years_`i'
	rename q404b_a_`i' q407b_a_months_`i'
	rename q405_a_`i' q407_a_continue_`i'
	
}

rename q401_a_other_1 q407_a_other_1
rename q401_a_other_C1_1 q407_a_other_C1_1 
rename q403_a_other_1 q407c_a_other_1
rename q403_a_other_2 q407c_a_other_2

rename q400_rpt_count q407_rpt_count
rename q401_final_lab q407_final_lab

** Survey Section 5
** Variables: 5.23-5.28 AND 5.29-5.36

** Convert: 5.7-5.10 -> 5.23-5.27
forval i = 1/19 {
	rename q507_`i' 			q523_`i'
	rename q507_prob_`i' 		q523_prob_`i'
	rename q508_`i' 			q524_`i'
	rename q508_other_`i' 		q524_other_`i'
	rename q508_other_C1_`i' 	q524_other_C1_`i'
	rename q508b_`i' 			q525_`i'

	rename q509_`i' 			q526a_`i'
	rename q510_`i' 			q527_`i'
	rename q510_1_`i' 			q527_1_`i'
	rename q510_2_`i' 			q527_2_`i'
	rename q510_3_`i' 			q527_3_`i'
	rename q510_4_`i' 			q527_4_`i'
	rename q510_5_`i' 			q527_5_`i'
	rename q510__96_`i' 		q527__96_`i'
	rename q510_other_`i' 		q527_other_`i'
	rename q510_other_C1_`i' 	q527_other_C1_`i'
	// 							Q28 // DNE
	// 							Q29 // PII
}
drop q526_2011_*
drop q526_2020_*
** Convert: 5.2.16-5.2.22 -> 5.30-5.36
forval i = 1/27 {
	rename q5_2_16_`i' 			q530_`i'
	rename q5_2_17_`i' 			q531_`i'
	rename q5_2_18_`i'			q532_`i'
	rename q5_2_19_`i' 			q533_`i'
	rename q5_2_20_`i' 			q534_`i'
	rename q5_2_21_`i' 			q535_`i'
	rename q5_2_21_other_`i' 	q535_other_1_`i'
	rename q5_2_21_other_C1_`i' q535_other_C1_1_`i'
	rename q5_2_22_`i' 			q536_`i'
	rename q5_2_22_Other_`i' 	q536_other_1_`i'
	rename q5_2_22_Other_C1_`i' q536_other_C1_1_`i'
}

drop children_for_edu_*		 //CTO calculation vars


** string names -> 5.1-5.10
drop member_gender_*
drop member_age_*
rename present_* 				q501_*
// rename not_member_reason_*	q502_* // PII
rename location_moved_*			q503_*
rename q504_* 					q506_q517_*
rename pre_r_slept_*			q504_* 
rename q505_*					q507_q518_*
rename q506_*					q522_*
rename pre_r_gender_*			q506_* 
rename pre_r_int_age_* 			q507_*
rename pre_r_age_live11jan_*  	q508_*
rename pre_r_age_live19oct_*  	q509_*   
rename pre_r_age_19oct_where_*  q510_*

** Variables: 5.11-5.22
rename hhr_new1					q511
rename q512 					q528
rename new_members_size1x		q512
rename new_members_size2x		q513
// 								q514 // PII
rename newmember_slept_prob_*	q516_*
rename newmember_slept_*		q515_*
rename hhr_new_gender_*			q517_*
rename hhr_new_age_int_*		q518_*
rename hhr_new_join_*			q519_*
rename hhr_new_other_*			q519_other_*
rename hhr_new_moved_year_*		q520_year_*
rename hhr_new_moved_month_*   q520_month_*
rename select_head_of_household	q521


** Survey Section 6
rename q603b q603a
rename q617c q620c
rename q621c_units_other q621a_units_other
rename q621c_units_other_C1 q621a_units_other_C1

** Survey Section 7
// local alphabet `c(ALPHA)'
// forval i = 1/9 { // rename 1-9 as a-i
// 	local l`i' : word `i' of `alphabet'
// 	dis "`l`i''"
// 	rename q7_1_1_`i' 		 q7_1_1_`l`i''
// 	rename q7_1_2_`i' 		 q7_1_2_`l`i''
// 	rename q7_1_3_amount_`i' q7_1_3_amount_`l`i''
// 	rename q7_1_4_amount_`i' q7_1_4_amount_`l`i''
// }

** Survey Section 10
// Not in data:   q10_2_02 and q10_2_27
// Not in survey: q11_2_34
rename q11* q10*
rename q10_1_13_* q10_1_continue_*
drop duration_10_*
rename duration_11_1 duration_10_1
rename duration_11_2 duration_10_2


** Survey Section 11
drop q1206a* q1206b* 

rename q12* q11*
rename duration_12_1 duration_11_1
rename duration_12_2 duration_11_2

** Survey Section 12
// local alphabet `c(ALPHA)'
// forval i = 1/6 { // rename 1-9 as a-i
// 	local l`i' : word `i' of `alphabet'
// 	dis "`l`i''"
// 	rename q1301_`i' q1301_`l`i''
// 	rename q1301_other_`i' q1301_other_`l`i''
// 	rename q1301_other_C1_`i' q1301_other_C1_`l`i''
// 	rename q1302_`i' q1302_`l`i''
// 	rename q1302_1_`i' q1302_1_`l`i''
// 	rename q1302_2_`i' q1302_2_`l`i''
// 	rename q1302_3_`i' q1302_3_`l`i''
// 	rename q1302__96_`i' q1302__96_`l`i''
// 	rename q1302_other_`i' q1302_other_`l`i''
// 	rename q1302_other_C1_`i' q1302_other_C1_`l`i''
// 	rename q1303_`i' q1303_`l`i''
// 	rename q1303_units_`i' q1303_units_`l`i''
// 	rename q1303_units_other_`i' q1303_units_other_`l`i''
// 	rename q1304_`i' q1304_`l`i''
// 	rename q1305_`i' q1305_`l`i''
// }
rename q13* q12*
rename s13* s12*
rename duration_13_* duration_12_*

** Survey Section 13
rename q14* q13*
rename q1305a* q1305z*_temp
rename q1305b* q1305a*
rename q1305c* q1305b*
rename q1305d* q1305c*
rename q1305e* q1305d*
rename q1305z*_temp q1305e*
rename duration_14_* duration_13_*

** Survey Section 16 (tempprary step)
rename q15_6_01 temp_q16_5H_01
rename q15_6_02a temp_q16_5H_02a
rename q15_6_02b temp_q16_5H_02b

** Survey Section 14
rename q15* q14*
rename duration_15_* duration_14_*

** Survey Section 15
// Not in data: q1503
rename q16* q15*
rename duration_16_* duration_15_*
drop q1501x_label1

** Survey Section 16
rename temp_q16_5H* q16_5H*
rename q17* q16*
rename duration_17_* duration_16_*
forval i = 3/22 { // rename as q-1
	local j = `i'-1
	if strlen("`i'") == 1 local i "0`i'"
	rename q16_1_`i'* q16_1_`j'*
}

rename q16_2_04 q16_2_02
rename q16_2_05 q16_2_03
rename q16_5_01 q16_5G_01
rename q16_5_02 q16_5G_02


** Survey Section 17
rename q18* q17*
rename duration_18_* duration_17_*
local alphabet `c(ALPHA)'
forval i = 0/7 { // rename 1-9 as a-i
	local counter = `i'+1
	local l`i' : word `counter' of `alphabet'
	dis "`l`i''"
	rename q17_3_04_`i' q17_3_04_`l`i''
}

** Survey Section 18
// Not in data: q1801a
rename q19* q18*
rename duration_19_* duration_18_*

** Survey Section 19
rename q200* q190*
rename duration_20_* duration_19_*

** Survey Section 20
// Not in data: Q20.16
rename q21* q20*
rename duration_21_* duration_20_*
rename q2021 q2023
rename q2020 q2022
rename q2020b q2021b
rename q2020a q2021a
rename q2019 q2020
rename q2018 q2019
rename q2017 q2018
rename q2016 q2017
rename q2015a* q2016*

** Survey Section 21
// Not in survey: q2215a_other_1
rename q22* q21*
rename duration_22_* duration_21_*

** Survey Section 22
rename q23* q22*
rename duration_23_* duration_22_*
rename q2215a* q2215*

** Survey Section 23
// Not in data: q2301, q2318
rename q24* q23*
rename duration_24_* duration_23_*
rename q2317_* 			q2318_*

** Survey Section 24
rename q25* q24*
rename duration_25_* duration_24_*
rename q2404 q2403
rename q2405 q2404
rename q2406a surveyed_in_residence

local label1 : label q402 1
local label2 : label q402 2
di "`label1'" "`label2'"

*****************************CLEANING***************************
**Changing all indicator variables to 0,1
quietly ds , has(type numeric)
foreach var in `r(varlist)' {
	quietly count if !inlist(`var',.,1,2)
	if `r(N)'==0 {
		assert inlist(`var',.,1,2)==1
		local label1 :  label  `var' 1
		local label2 :  label  `var' 2
		if "`label1'"=="Yes" & "`label2'"=="No" {
			replace `var' = 0 if `var'==2 
			assert inlist(`var',.,1,0)
			lab define `var' 0 "No" 1 "Yes", replace 
			lab values `var' `var'
		}
	}
	else {
		quietly count if !inlist(`var',.,1,2,-99)
		if `r(N)'==0 {
			assert inlist(`var',.,1,2,-99)==1
			local label1 :  label  `var' 1
			local label2 :  label  `var' 2
			local label9 : label `var' -99
			if "`label1'"=="Yes" & "`label2'"=="No" {
				replace `var' = 0 if `var'==2 
				assert inlist(`var',.,1,0,-99)
				lab define `var' 0 "No" 1 "Yes" -99 "`label9'", replace 
				lab values `var' `var'
				tab `var',mi nolab
			}
		}
	}
}
replace q402 = 0 if q402 == 2
label def q402 0 "No" 1 "Yes"
label val q402 q402

foreach var of varlist q506_? q506_??{
	lab def `var' 2 " Female" 1 "Male", replace
	lab val `var' `var'
}
**Dropping numeric variables with 0 obs
foreach var of varlist _all {
	capture assert mi(`var')
	if !_rc {
		drop `var'
	}
}

**Remove hidden spaces from string variables
ds, has (type string)
local strvars `r(varlist)' 
foreach var of varlist `r(varlist)' {
	replace `var' = strtrim(`var')
}

**Droping all variables with only "." and all string variables with 0 obs
missings dropvars, force

*destring everything that can be destrung (this takes a while)
ds, has(type string) 
local strvars "`r(varlist)'"
destring `strvars', replace 

ds, has(type string)
local strvars "`r(varlist)'"
foreach var of varlist `strvars' {
	replace `var' = strtrim(`var')
}


*drop number of individuals under 19
cap drop __00000?
egen n_kids = rowtotal(under19_counter_*)
lab var n_kids "Number of individual <19 years old in hh"

**drop redundant variable
assert q601a== q601a_null
drop  q601a_null

**everyone was surveyed about the same sections in the same order,
**so these variables are safe to drop
forv n=1/9 {
	assert s7_1_index_`n'==`n' if !mi(s7_1_index_`n')
	drop s7_1_index_`n' 
}

forv n=1/8 {
	tab s9_item_eng_`n'
	assert s9_index_`n'==`n' if !mi(s9_index_`n')
	drop s9_index_`n' s9_item_eng_`n'
}



** Section 10.2 cleaning **
**add codes for freelancer, gas station attendent/car wash, porter (G9 codes)
**80  Driver (public or private) (delivery work)
replace q10_2_03_1 = 24 if q10_2_03_1==-96 & ustrregexm(q10_2_03_other_1,"مساعد مدرس")


forv n=1/3 {
  label define q10_2_03_`n' 86 "Freelancer" 87 "Porter" 88 "Gas station/car wash attendant", add
  lab values q10_2_03_`n' q10_2_03_`n'
}
forv n=1/3 {
  quietly count if q10_2_03_`n'==-96 
  if `r(N)'!=0 {
	**freelancer ﻩﺮﺣ ﻝﺎﻤﻋﺃ
    replace q10_2_03_`n' = 86 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n',"أعمال حره")
    replace q10_2_03_`n' = 86 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n',"مشاغل")
    replace q10_2_03_`n' = 86 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n',"غير منتظم")
    replace q10_2_03_`n' = 86 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n',"عمل حر")
    replace q10_2_03_`n' = 86 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n',"عامل حر")
	
	**porter ﻝﺎﺘﻋ
    replace q10_2_03_`n' = 87 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n',"عتال")
    replace q10_2_03_`n' = 87 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n'," تحميل")
    replace q10_2_03_`n' = 87 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n'," التحميل")
    replace q10_2_03_`n' = 87 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n'," نقل المياه")
	
	**gas station/car wash attendent ﺕﺎﻗﻭﺮﺤﻣ ﻪﻄﺤﻣ|ﺕاﺭﺎﻴﺳ ﻞﻴﺴﻏ
    replace q10_2_03_`n' = 88 if q10_2_03_`n'==-96 & ustrregexm(q10_2_03_other_`n',"محطه محروقات|غسيل سيارات")
  }
}

label define q10_2_04_1 21 "Freelancing", add 
lab values q10_2_04_1 q10_2_04_1
replace q10_2_04_1=21 if q10_2_04_1==-96 & q10_2_04_other_1=="لا يوجد مجال محدد"

**WHY NOT LOOKING FOR WORK
label define q10_2_16 15  "Old age" 16  "Refusal from family to work" ///
   22  "UNHCR Not Allowing More Than One Person per Family to Work", add
lab values q10_2_16 q10_2_16
decode q10_2_16_other_C1, gen(trash)


replace q10_2_16 = 12 if trash == "In University"
replace q10_2_16 = 15 if trash == "Due to Age"
replace q10_2_16 = 16 if trash == "Family Rejection of the Idea of Working"
replace q10_2_16 = 22 if trash == "UNHCR Not Allowing More Than One Person per Family to Work"
drop trash

***************************Drop CTO generated variables*********
drop member* previous_round* preround* rid* individual* pre_r2_confirm* ///
	activity* newhhmember* new_member_* over* filter* *index* hhid* ///
	current_mem* *under* *male* *boy* *girl* five* sum* *head*  *randnum* ///
	*repeat* *members* keep* *child* more* fr_bio* *select* indiv* calc* agree* ///
	age_diff* s7*  s8* s9* section* s12* gender_numi* 
	
	
************************Drop internal variable************************
/* these variables are not currently publically available, but will be in future versions of the data*/
drop q1501* q1524* q1530* q1532* q16_1_19* q16_1_20* q16_1_21* q16_2_01* q16_2_02 q16_2_03 q16_3_01* q16_4_0* q17_1_* q1526 q1528 q1525* q1522* q1527 q1528 q1529

** save
save "${dir}/intermediate_public/panel_F2F_2024_deidentified_clean.dta", replace
